﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Entities = Vision.Core.Entities;

namespace Vision.VisionWorkshop.Windows
{
    /// <summary>
    /// Interaction logic for ActivityOpen.xaml
    /// </summary>
    public partial class ActivityOpen : Window
    {
        public delegate void ActivitySelected(Entities.Activities.VisionActivity enterpriseActivity);
        public event ActivitySelected ActivitySelectedEvent;

        private bool _loaded = false;
        private bool _closing = false;
        
        /// <summary>
        /// 
        /// </summary>
        public ActivityOpen()
        {
            InitializeComponent();

            this.Closing += new System.ComponentModel.CancelEventHandler(ActivityOpen_Closing);
            this.LocationChanged += new EventHandler(ActivityOpen_LocationChanged);
            this.SizeChanged += new SizeChangedEventHandler(ActivityOpen_SizeChanged);
        }

        void ActivityOpen_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _closing = true;

            this.Closing -= new System.ComponentModel.CancelEventHandler(ActivityOpen_Closing);
            this.LocationChanged -= new EventHandler(ActivityOpen_LocationChanged);
            this.SizeChanged -= new SizeChangedEventHandler(ActivityOpen_SizeChanged);
        }

        void ActivityOpen_LocationChanged(object sender, EventArgs e)
        {
            if (_loaded && !_closing)
                Common.ApplicationSettings.FileOpen_Window_Location = new Point(this.Left, this.Top);
        }

        void ActivityOpen_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (_loaded && !_closing)
                Common.ApplicationSettings.FileOpen_Window_Size = e.NewSize;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Animation.Storyboard sbLoading = this.FindResource("sbLoading") as System.Windows.Media.Animation.Storyboard;
            sbLoading.Begin();

            resizeFromStorage();

            _loaded = true;

            System.ComponentModel.BackgroundWorker backgroundWorker = new System.ComponentModel.BackgroundWorker();
            backgroundWorker.DoWork += delegate(object s, System.ComponentModel.DoWorkEventArgs args)
            {
                svcVision.VisionServiceClient visionClient = new svcVision.VisionServiceClient();
                List<Entities.Filing.Folder> folders = new List<Entities.Filing.Folder>();
                Entities.Filing.Folder rootFolder = visionClient.GetFolders();
                if (rootFolder != null){
                    folders.Add(rootFolder);
                }

                Dispatcher.BeginInvoke(
                    new Action(delegate()
                    {
                        this.tvActivities.ItemsSource = folders;

                        sbLoading.Stop();
                    }
                ));
            };

            // begin a background worker process to open the activities
            backgroundWorker.RunWorkerAsync();
        }

        private void resizeFromStorage()
        {
            // Location
            if (Common.ApplicationSettings.FileOpen_Window_Location.HasValue)
            {
                this.Left = Math.Max(0, Common.ApplicationSettings.FileOpen_Window_Location.Value.X);
                this.Top = Math.Max(0, Common.ApplicationSettings.FileOpen_Window_Location.Value.Y);
            }

            // Size
            if (Common.ApplicationSettings.FileOpen_Window_Size.HasValue)
            {
                this.Width = Math.Max(100, Common.ApplicationSettings.FileOpen_Window_Size.Value.Width);
                this.Height = Math.Max(100, Common.ApplicationSettings.FileOpen_Window_Size.Value.Height);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvActivities_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            this.lvActivities.ItemsSource = null;
            Entities.Filing.Folder folder = (Entities.Filing.Folder)e.NewValue;
            if (folder != null){
                this.lvActivities.ItemsSource = folder.Activities.OrderBy(a => a.Name);                  
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvActivities_ItemDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Entities.Activities.VisionActivity enterpriseActivity = ((FrameworkElement)sender).DataContext as Entities.Activities.VisionActivity;
            if (enterpriseActivity != null & this.ActivitySelectedEvent != null){
                svcVision.VisionServiceClient visionClient = new svcVision.VisionServiceClient();
                enterpriseActivity = visionClient.GetVisionActivityFromCatalog(enterpriseActivity.Id);
                this.ActivitySelectedEvent(enterpriseActivity);
                this.Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (this.lvActivities.SelectedItem != null){
                Entities.Activities.VisionActivity enterpriseActivity = (Entities.Activities.VisionActivity)this.lvActivities.SelectedItem;
                if (enterpriseActivity != null & this.ActivitySelectedEvent != null){
                    svcVision.VisionServiceClient visionClient = new svcVision.VisionServiceClient();
                    enterpriseActivity = visionClient.GetVisionActivityFromCatalog(enterpriseActivity.Id);
                    this.ActivitySelectedEvent(enterpriseActivity);                        
                }
            }
            this.Close();            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }        
    }
}
